//ST*****************************************单选按钮*****************************************
function JyyRadio() {
}

JyyRadio.init = function (id, groupName, checked, value, text, style, callback) {
    var ctrl = document.getElementById(id);
    if (ctrl == undefined || ctrl == null || ctrl == "") {
        ctrl = document.createElement("div");
    }
	ctrl.dispose = function(isAll) {
	    while (this.hasChildNodes()) {
	        this.removeChild(this.lastChild);
	    }
	    if (isAll == true) {
	        var parentNode = this.parentNode;
	        if (parentNode) {
	            parentNode.removeChild(this);
	        }
	    }
	};
	
	ctrl.ce = function(parent, id, tagName, className) {
		var ctrl = document.createElement(tagName);
		ctrl._self = this;
		ctrl._owner = parent;
		if (id) {
			ctrl.id = id;
		}
		if (className) {
			ctrl.className = className;
		}
		if (parent) {
			parent.appendChild(ctrl);
		}
		return ctrl;
	};
	
	ctrl.disableSelection = function(target) {
	    if (target) {
	        if (typeof target.onselectstart) {
	            target.onselectstart = function() {
	                return false;
	            };
	        } else if (typeof target.MozUserSelect) {
	            target.MozUserSelect = "none";
	        } else {
	            target.onmousedown = function() {
	                return false;
	            };
	        }
	    }
	};
	
	ctrl.setStyle = function(ctrl, style) {
		if (!ctrl) {
			return;
		}
	    var cssText = "";
	    for (var prop in style) {
	        ctrl.style[prop] = style[prop];
	    }
	};
    ctrl.dispose();

	if (id) {
    	ctrl.id = id;
	}
	ctrl.rxtype = "JyyRadio";
    ctrl.groupName = groupName;
    ctrl.controlType = "jyyradio";
    ctrl.value = value;
    ctrl.text = text;
    ctrl.callback = callback;

    if (style) {
        ctrl.setStyle(ctrl, style);
    }

	ctrl.ce = ctrl.ce;

    ctrl.label = ctrl.ce(ctrl,null, "div", "jyyradio_text");
    ctrl.label.innerHTML = text;
    ctrl.disableSelection(ctrl.label);

    ctrl.checkBoxclick = function() {
        this.checked = true;
        this.refresh();
        if (this.callback) {
			var rxevent = {};
			rxevent.id = this.id;
			rxevent.name = "click";
			rxevent.self = this._self;
			rxevent.target = this;
			rxevent.source = this;
			rxevent.value = this.value;
			rxevent.text = this.text;
			rxevent.checked = this.checked;
            this.callback(rxevent);
        }
    };
    ctrl.onclick = ctrl.checkBoxclick;
    ctrl.disable = function(isDisable) {
    	if(isDisable){
    		this.onclick = function(){};
    		this.label.className = "jyyradio_text_disable"
    	}else{
    		this.onclick = ctrl.checkBoxclick;
    		this.label.className = "jyyradio_text";
    	}
    }
    ctrl.refresh = function() {
        if (this.checked == true) {
            this.className = "jyyradio_on";
        } else {
            this.className = "jyyradio_off";
            return;
        }

        var data = this.getRadios();
        for (var i = 0; i < data.radios.length; i++) {
            if (this == data.radios[i]) {
            } else {
                data.radios[i].checked = false;
                data.radios[i].className = "jyyradio_off";
            }
        }
    };

    ctrl.getValue = function() {
        var data = this.getRadios();
        if (data.self) {
            return data.self.value;
        }
        return null;
    };

    ctrl.getData = function() {
        var data = this.getRadios();
        if (data.self) {
            return {code:data.self.value,name:data.self.text};
        }
        return null;
    };

    ctrl.getRadios = function() {
        var data = {self:null, radios:[]};
        var radios = $(".jyyradio_on,.jyyradio_off");
        for (var i = 0; i < radios.length; i++) {
            if (radios[i].groupName == this.groupName) {
                data.radios[data.radios.length] = radios[i];
                if (radios[i].checked) {
                	data.self = radios[i];
                }
            }
        }
        return data;
    };

    ctrl.checked = (checked==true) ? true : false;
    ctrl.refresh();

    return ctrl;
};
//ED*****************************************单选按钮*****************************************
